// pages/notice-publish/notice-publish.js
Page({
  data: {
    form: {
      title: '',
      content: '',
      status: '已发布',
      publisherId: ''
    },
    isSubmitting: false
  },

  onLoad() {
    // 从Storage获取发布者ID
    const userInfo = wx.getStorageSync('username');
    // console.log('===========',userInfo);
    this.setData({
      'form.publisherId': userInfo
    });
    // console.log('===========',this.data.form.publisher_id);
  },

  // 输入标题
  onTitleInput(e) {
    this.setData({
      'form.title': e.detail.value
    });
  },

  // 输入内容
  onContentInput(e) {
    this.setData({
      'form.content': e.detail.value
    });
  },

  // 切换状态
  changeStatus(e) {
    const status = e.currentTarget.dataset.status;
    this.setData({
      'form.status': status
    });
  },

  // 提交表单
  handleSubmit() {
    const { title, content } = this.data.form;

    if (!title.trim()) {
      wx.showToast({ title: '请填写通知标题', icon: 'none' });
      return;
    }

    if (!content.trim()) {
      wx.showToast({ title: '请填写通知内容', icon: 'none' });
      return;
    }

    this.setData({ isSubmitting: true });

    // 调用提交API
    wx.request({
      url: 'http://192.168.172.134:5000/api/addNotice',
      method: 'GET',
      data: this.data.form,
      success: (res) => {
        if (res.data.code === 200) {
          wx.showToast({
            title: this.data.form.status === '已发布' ? '发布成功' : '保存成功',
            icon: 'success'
          });
          wx.navigateBack();
        }
      },
      complete: () => {
        this.setData({ isSubmitting: false });
      }
    });
  }
});